package cn.gnp.framework.eeamis.service.goods.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import cn.gnp.framework.persist.Constants;
import cn.gnp.framework.persist.pagination.Page;
import cn.gnp.framework.persist.pagination.PageUtil;

import cn.gnp.framework.service.impl.BaseServiceImpl;
import cn.gnp.framework.eeamis.dao.goods.IGoodsCodeDao;
import cn.gnp.framework.core.exception.NotCodeException;
import cn.gnp.framework.eeamis.model.goods.GoodsCodeModel;
import cn.gnp.framework.eeamis.model.goods.GoodsModel;
import cn.gnp.framework.eeamis.service.goods.IGoodsCodeService;
import cn.gnp.framework.eeamis.service.goods.IGoodsService;
@Service
public class GoodsCodeServiceImpl extends BaseServiceImpl<GoodsCodeModel, Integer> implements IGoodsCodeService {
   
    private IGoodsService goodsService;
    
    public Page<GoodsCodeModel> listAllByGoods(int pn, int goodsId) {
        Integer count = getGoodsCodeDao().countAllByGoods(goodsId);
        List<GoodsCodeModel> items = getGoodsCodeDao().listAllByGoods(pn, goodsId);
        return PageUtil.getPage(count, pn, items, Constants.DEFAULT_PAGE_SIZE);
    }
    
    private IGoodsCodeDao getGoodsCodeDao() {
        return (IGoodsCodeDao) getDao();
    }

    public void save(int goodsId, String[] codes) {
        GoodsModel goods = goodsService.get(goodsId);
        for(String code : codes) {
            if(StringUtils.hasText(code)) {
                GoodsCodeModel goodsCode = new GoodsCodeModel();
                goodsCode.setCode(code);
                goodsCode.setGoods(goods);
                save(goodsCode);
            }
        }
    }
    

    @Autowired @Required 
    public void setGoodsService(IGoodsService goodsService) {
        this.goodsService = goodsService;
    }
    
    @Autowired @Required
    public void setGoodsCodeDao(IGoodsCodeDao dao) {
        setDao(dao);
    }

	public GoodsCodeModel buy(String username, int goodsId)
			throws NotCodeException {
		// TODO Auto-generated method stub
		return null;
	}
}
